package com.dynamixsoftware.printershare.bjnp;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

public class BJNPSocket {
	private OutputStream bjnp_os = new OutputStream() {
		private byte[] buf = new byte[4096];
		private int pos;

		public void close() throws IOException {
			flush();
		}
		public void flush() throws IOException {
            if(this.pos != 0) {
               int var1 = 0;
               long var2 = System.currentTimeMillis();

               while(System.currentTimeMillis() - var2 < 180000L) {
                  BJNPCommand var10000 = new BJNPCommand();
                  BJNPCommand var4 = var10000;
                  var4.dev_type = 1;
                  var4.cmd_code = 33;
                  var4.seq_no = BJNPMain.get_packet_id();
                  var4.session_id = BJNPSocket.this.session_id;
                  if(var1 == 0) {
                     var4.dlen = this.pos;
                     var4.data = this.buf;
                  } else {
                     var4.dlen = this.pos - var1;
                     var4.data = new byte[var4.dlen];
                     System.arraycopy(this.buf, var1, var4.data, 0, var4.dlen);
                  }

                  BJNPSocket.this.sos.write(var4.toPacket());
                  BJNPSocket.this.sos.flush();
                  BJNPCommand var5 = BJNPCommand.fromStream(BJNPSocket.this.sis);
                  if(var5 == null) {
                     break;
                  }

                  if(var5.error_code != 0) {
                     throw new IOException("Protocol error " + var5.error_code + ".");
                  }

                  var1 += ((255 & var5.data[0]) << 24) + ((255 & var5.data[1]) << 16) + ((255 & var5.data[2]) << 8) + (255 & var5.data[3]);
                  if(var1 == this.pos) {
                     this.pos = 0;
                     return;
                  }

                  try {
                     Thread.sleep(1000L);
                  } catch (InterruptedException var7) {
                     ;
                  }
               }

               throw new IOException("Network or printer error.");
            }
         }

		public void write(int paramAnonymousInt) throws IOException {
			byte[] arrayOfByte = this.buf;
			int i = this.pos;
			this.pos = (i + 1);
			arrayOfByte[i] = ((byte) paramAnonymousInt);
			if (this.pos >= this.buf.length) {
				flush();
			}
		}

		public void write(byte[] paramAnonymousArrayOfByte) throws IOException {
			write(paramAnonymousArrayOfByte, 0, paramAnonymousArrayOfByte.length);
		}

		public void write(byte[] paramAnonymousArrayOfByte, int paramAnonymousInt1, int paramAnonymousInt2)
				throws IOException {
			while (paramAnonymousInt2 > 0) {
				int i = this.buf.length - this.pos;
				if (i > paramAnonymousInt2) {
					i = paramAnonymousInt2;
				}
				System.arraycopy(paramAnonymousArrayOfByte, paramAnonymousInt1, this.buf, this.pos, i);
				paramAnonymousInt2 -= i;
				paramAnonymousInt1 += i;
				this.pos = (i + this.pos);
				if (this.pos >= this.buf.length) {
					flush();
				}
			}
		}
	};
	private int session_id;
	private InputStream sis;
	private Socket socket;
	private OutputStream sos;

	
	private boolean connect(InetSocketAddress var1, int var2, boolean var3) throws Exception {
	      int var4 = 0;
	      int var5 = 0;
	      long var6 = System.currentTimeMillis();

	      while(System.currentTimeMillis() - var6 < (long)var2) {
	         BJNPCommand var10000 = new BJNPCommand();
	         BJNPCommand var8 = var10000;
	         var8.dev_type = 1;
	         var8.cmd_code = 16;
	         byte[] var9 = "android ".getBytes("UTF-16LE");
	         byte[] var10 = "android_user".getBytes("UTF-16LE");
	         byte[] var11 = "android_document".getBytes("UTF-16LE");
	         var8.data = new byte[392];
	         var8.data[0] = (byte)(255 & var4 >> 24);
	         var8.data[1] = (byte)(255 & var4 >> 16);
	         var8.data[2] = (byte)(255 & var4 >> 8);
	         var8.data[3] = (byte)(var4 & 255);
	         var8.data[4] = (byte)(255 & var5 >> 24);
	         var8.data[5] = (byte)(255 & var5 >> 16);
	         var8.data[6] = (byte)(255 & var5 >> 8);
	         var8.data[7] = (byte)(var5 & 255);
	         byte[] var12 = var8.data;
	         int var13;
	         if(var9.length > 64) {
	            var13 = 63;
	         } else {
	            var13 = -1 + var9.length;
	         }

	         System.arraycopy(var9, 0, var12, 9, var13);
	         byte[] var14 = var8.data;
	         int var15;
	         if(var10.length > 64) {
	            var15 = 63;
	         } else {
	            var15 = -1 + var10.length;
	         }

	         System.arraycopy(var10, 0, var14, 73, var15);
	         byte[] var16 = var8.data;
	         int var17;
	         if(var11.length > 256) {
	            var17 = 255;
	         } else {
	            var17 = -1 + var11.length;
	         }

	         System.arraycopy(var11, 0, var16, 137, var17);
	         BJNPCommand var18 = BJNPMain.sendUDPCommand(var8, var1, 1000);
	         if(var18 != null) {
	            if(var18.error_code == 0 && var18.session_id != 0) {
	               if(!var3) {
	                  this.session_id = var18.session_id;
	                  this.socket = new Socket();
	                  this.socket.connect(var1, var2);
	                  this.sis = this.socket.getInputStream();
	                  this.sos = this.socket.getOutputStream();
	               }

	               return true;
	            }

	            if(var18.error_code != '\u8502') {
	               throw new IOException("Protocol error " + var18.error_code + ".");
	            }

	            if(var3) {
	               return true;
	            }

	            ++var4;
	            var5 = ((255 & var18.data[0]) << 24) + ((255 & var18.data[1]) << 16) + ((255 & var18.data[2]) << 8) + (255 & var18.data[3]);

	            try {
	               Thread.sleep(1000L);
	            } catch (InterruptedException var20) {
	               ;
	            }
	         }
	      }

	      return false;
	   }

	public boolean check(InetSocketAddress paramInetSocketAddress, int paramInt) throws Exception {
		return connect(paramInetSocketAddress, paramInt, true);
	}

	public void close() throws Exception {
		if (this.socket != null) {
			this.bjnp_os.close();
			BJNPCommand localBJNPCommand = new BJNPCommand();
			localBJNPCommand.dev_type = 1;
			localBJNPCommand.cmd_code = 17;
			localBJNPCommand.seq_no = BJNPMain.get_packet_id();
			localBJNPCommand.session_id = this.session_id;
			this.sos.write(localBJNPCommand.toPacket());
			this.sos.flush();
			this.socket.close();
			this.socket = null;
			this.sis = null;
			this.sos = null;
		}
	}

	public void connect(InetSocketAddress paramInetSocketAddress, int paramInt) throws Exception {
		if (!connect(paramInetSocketAddress, paramInt, false)) {
			throw new IOException("Network or printer error.");
		}
	}

	public OutputStream getOutputStream() {
		return this.bjnp_os;
	}

	public void setSoTimeout(int paramInt) throws IOException {
		if (this.socket != null) {
			this.socket.setSoTimeout(paramInt);
		}
	}
}

/*
 * Location:
 * C:\Users\Admin\Desktop\Decompiler\JAD\jd-gui-0.3.6.windows\classes_dex2jar
 * .jar
 * 
 * Qualified Name: com.dynamixsoftware.printershare.bjnp.BJNPSocket
 * 
 * JD-Core Version: 0.7.0.1
 */